package com.itrip.controller;


import com.itrip.dto.OrderInfoDTO;
import com.itrip.dto.OrderPayDTO;
import com.itrip.dto.ResponseDTO;
import com.itrip.pojo.ItripUser;
import com.itrip.service.OrderService;
import com.itrip.vo.pay.OrderInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = {"用于处理订单事务"})
@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation("查看顾客是够有未支付订单")
    @ApiImplicitParam(name = "token",value = "登陆令牌",dataType = "String",paramType = "header")
    @PostMapping("/view/CheckOrder")
    public ResponseDTO CheckOrder(HttpServletRequest request){
        String token = request.getHeader("token");
        ItripUser itripUser =(ItripUser)redisTemplate.opsForValue().get(token);
        Long id = itripUser.getId();
        Integer integer = orderService.CheckOrder(id);
        if (integer>0){
            return new ResponseDTO(101,"有未支付的订单",integer);
        }
        return new ResponseDTO(100,"没有未支付的订单",null);
    }

    @ApiOperation("传递订单号给前端支付页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeNo",value = "订单号",example = "D100000120171110777777777ed"),
            @ApiImplicitParam(name="token",value="登陆令牌",dataType = "String",paramType = "header")
            //可以通过令牌的id获取RradeNo，但由于测试数据的原因，现在依然使用tradeNo进行数据传输
    })
    @GetMapping("/sendTradeNo")
    public ResponseDTO sendTradeNo(String tradeNo,HttpServletRequest request){
        OrderPayDTO orderPayDTO= orderService.sendTradeNo(tradeNo);
        if(orderPayDTO==null)
            return new ResponseDTO(101,"订单不存在",null);
        return new ResponseDTO(100,"订单相关支付",orderPayDTO);
    }


    @ApiOperation("查询订单信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户登陆令牌",dataType = "String",paramType = "header")
    })
    @PostMapping("/view/selectOrderInfo")
    public ResponseDTO selectOrderInfo(OrderInfoVo orderInfoVo){
        //从第几个开始
        int start=(orderInfoVo.getPageNow()-1)*orderInfoVo.getPageSize();
        List<OrderInfoDTO> orderInfoDTOS=orderService.selectOrderInfo(orderInfoVo,start);
        return new ResponseDTO(100,"查询成功",orderInfoDTOS);
    }


    @ApiOperation("查询订单数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户登陆令牌",dataType = "String",paramType = "header")
    })
    @PostMapping("/view/selectCountOfOrderInfo")
    public ResponseDTO selectCountOfOrderInfo(OrderInfoVo orderInfoVo){
        int count = orderService.selectCountOfOrderInfo(orderInfoVo);
        return new ResponseDTO(100,"数量查询成功",count);
    }

    @ApiOperation("获取room的最新订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户登陆令牌",dataType = "String",paramType = "header"),
            @ApiImplicitParam(name="roomId",value = "房间id")
    })
    @PostMapping("/view/selectOrderStatus")
    public ResponseDTO selectOrderStatus(Long roomId){
        int roomStatus = orderService.selectOrderStatus(roomId);
        return new ResponseDTO(100,"查询订单状态成功",roomStatus);
    }


    @ApiOperation("删除订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户登陆令牌",dataType = "String",paramType = "header"),
            @ApiImplicitParam(name="orderNo",value = "订单号")
    })
    @PostMapping("/view/deleteOrder")
    public ResponseDTO deleteOrder(String orderNo){
        int count=orderService.deleteOrder(orderNo);
        return new ResponseDTO(100,"删除成功",count);
    }





}